package hns.services;

import hns.Bot;
import hns.Service;

import java.io.File;
import java.net.UnknownHostException;

public class SysinfoService implements Service {
	String command = "!sysinfo";

	
	public SysinfoService(){
		Bot.getInstance().addService(getCommandName(), this);
	}
	
	@Override
	public String getCommandName() {
		return command;
	}

	@Override
	public void onMessage(String channel, String sender, String login, String hostname, String message) {
		
		String[] params = message.split(" ");
		
		
		if(params.length < 2){
			Bot.getInstance().sendMessage(channel, command + " syntax: "+command+" processors|memory|diskspace|hostname");
			return;
		}
		
		switch(params[1]){
		case "processors":
		   	Bot.getInstance().sendMessage(channel, "Available processors (cores): " + 
		        Runtime.getRuntime().availableProcessors());
		   	break;
		case "memory":
			Bot.getInstance().sendMessage(channel, "JVM Memory available: "+Runtime.getRuntime().freeMemory()/1024/1024+"mb/"+Runtime.getRuntime().maxMemory()/1024/1024+"mb");
			break;
		case "diskspace":
		    /* Get a list of all filesystem roots on this system */
		    File[] roots = File.listRoots();

		    /* For each filesystem root, print some info */
		    for (File root : roots) {
		      Bot.getInstance().sendMessage(channel, "File system root: " + root.getAbsolutePath());
		      Bot.getInstance().sendMessage(channel, "Free space: " + root.getFreeSpace()/1024/1024/1024+ "/" + root.getTotalSpace()/1024/1024/1024+"gb");
		    }
		    break;
		case "hostname":
			try {
				Bot.getInstance().sendMessage(channel, "Hostname: "+java.net.InetAddress.getLocalHost().getHostName());
			} catch (UnknownHostException e) {
				Bot.getInstance().sendMessage(channel, e.getMessage());
				e.printStackTrace();
			}
		    break;
		default:
			Bot.getInstance().sendMessage(channel, command + " syntax: "+command+" processors|memory|diskspace|hostname");
		}


	}

	@Override
	public void onJoin(String channel, String sender, String login,
			String hostname) {
		// TODO Auto-generated method stub

	}

}
